// <copyright file="EasyWebApp.cs" company="<Customer Name>">This is a "work-for-hire". or Copyright (C) 2010 MicroCODE Incorporated Troy, MI</copyright><author><Class Author's Name></author>

/*
 *      Title:    <Class Description>
 *      Module:   ClassTemplate (<Namespace>:ClassTemplate.cs)
 *      Project:  <Project Name>
 *      Customer: <Customer Name>
 *      Facility: MicroCODE Incorporated
 *      Date:     <Class Creation Date>
 *      Author:   <Class Author's Name>
 * 
 * 
 * 
 *  
 *      MODIFICATIONS:
 *      --------------
 *
 *  Date:         By-Group:   Rev:     Description:
 *
 *  DD-Mmm-YYYY   XYZ-GROUP  {0001}    Describe reason for creating or modifying class... 
 * 
 * 
 * 
 */
using System;
using Org.EasyWeb.Cache;
using Org.EasyWeb.Data;
// <copyright file="EasyWebApp.cs" company="<Customer Name>">This is a "work-for-hire". or Copyright (C) 2010 </copyright><author><Class Author's Name></author>

/*
 *      Title:    <Class Description>
 *      Module:   ClassTemplate (<Namespace>:ClassTemplate.cs)
 *      Project:  <Project Name>
 *      Customer: <Customer Name>
 *      Facility: MicroCODE Incorporated
 *      Date:     <Class Creation Date>
 *      Author:   <Class Author's Name>
 * 
 * 
 * 
 *  
 *      MODIFICATIONS:
 *      --------------
 *
 *  Date:         By-Group:   Rev:     Description:
 *
 *  DD-Mmm-YYYY   XYZ-GROUP  {0001}    Describe reason for creating or modifying class... 
 * 
 * 
 * 
 */
using Org.EasyWeb.Log;
using Org.EasyWeb.Right;
using Org.EasyWeb.Sys;
using Org.EasyWeb.Xml;

namespace Org.EasyWeb.Web
{
    public class SessionGlobal : ISessionGlobal
    {
        // Fields
        private CachesHashTable fCaches;
        private IDoubleTransformAll fDoubleTransform;
        private XmlEmptySource fEmptySource;
        private ErrorPageSource fErrorPage;
        private object fGuid;
        private XsltGroup fIeFiles = new XsltGroup();
        private IUserInfo fInfo;
        private LogService fLog;
        private XsltGroup fNavFiles = new XsltGroup();
        private RightCollection fRights;
        private ITransformAll fTransform;
        private string fXsltPath;

        // Methods
        public SessionGlobal()
        {
            this.fXsltPath = this.fIeFiles.Path = this.fNavFiles.Path = AppSettings.Current.XsltPath;
            this.fInfo = new UserInfo();
            this.fEmptySource = new XmlEmptySource();
            this.fErrorPage = new ErrorPageSource();
            this.fRights = new RightCollection();
            this.fCaches = new SessionCachesHashTable();
            this.fLog = new LogService();
            this.SetGuid();
            this.fInfo.EncodingChange += new EventHandler(this.SetEncoding);
        }

        private void SetDoubleTransformProps()
        {
            for (HtmlPosition position = HtmlPosition.Head; position <= HtmlPosition.Bottom; position += 1)
            {
                this.fDoubleTransform[position] = this.fEmptySource;
            }
            this.IeFiles.AssignTo(this.fDoubleTransform.IeFiles);            
        }

        private void SetEncoding(object sender, EventArgs e)
        {
            for (HtmlPosition position = HtmlPosition.Head; position <= HtmlPosition.Bottom; position += 1)
            {
                IXmlSource source = this.fTransform[position];
                if (source != null)
                {
                    source.Encoding = this.fInfo.Encoding;
                }
            }
            this.fErrorPage.Encoding = this.fInfo.Encoding;
        }

        public void SetGuid()
        {
            this.fGuid = System.Guid.NewGuid().ToString();
        }

        private void SetTransformProps()
        {
            for (HtmlPosition position = HtmlPosition.Head; position <= HtmlPosition.Bottom; position += 1)
            {
                this.fTransform[position] = this.fEmptySource;
            }
            this.IeFiles.AssignTo(this.fTransform.IeFiles);          
        }

        // Properties
        public CachesHashTable Caches
        {
            get
            {
                return this.fCaches;
            }
        }

        public IDoubleTransformAll DoubleTransform
        {
            get
            {
                return this.fDoubleTransform;
            }
            set
            {
                this.fDoubleTransform = value;
                this.SetDoubleTransformProps();
            }
        }

        public ErrorPageSource ErrorPage
        {
            get
            {
                return this.fErrorPage;
            }
        }

        public object Guid
        {
            get
            {
                return this.fGuid;
            }
        }

        public XsltGroup IeFiles
        {
            get
            {
                return this.fIeFiles;
            }
        }

        public IUserInfo Info
        {
            get
            {
                return this.fInfo;
            }
        }

        public LogService Log
        {
            get
            {
                return this.fLog;
            }
        }

        public XsltGroup NavFiles
        {
            get
            {
                return this.fNavFiles;
            }
        }

        public RightCollection Rights
        {
            get
            {
                return this.fRights;
            }
        }

        public ITransformAll Transform
        {
            get
            {
                return this.fTransform;
            }
            set
            {
                this.fTransform = value;
                this.SetTransformProps();
            }
        }

        public string XsltPath
        {
            get
            {
                return this.fXsltPath;
            }
        }
    }
}
